//
//  RegistrationViewModel.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/4/21.
//  Copyright © 2020 QDSG. All rights reserved.
//

import UIKit
import HandyJSON

enum RegistrationStatus: Int {
    case sucess = 401
    case cancel = 402
    case deadline = 403
    case error = -1
    
    var text: String {
        switch self {
        case .sucess: return "已预约"
        case .cancel: return "已取消"
        case .deadline: return "已过期"
        default: return ""
        }
    }
    
    var statusTextColor: UIColor {
        switch self {
        case .sucess: return .mainColor
        case .cancel: return .textLight
        case .deadline: return .textSupLight
        default: return .textSupLight
        }
    }
    
    var doctorTextColor: UIColor {
        switch self {
        case .sucess: return .textDark
        case .cancel: return .textLight
        case .deadline: return .textSupLight
        default: return .textSupLight
        }
    }
    
    var deptTextColor: UIColor {
        switch self {
        case .sucess: return .mainColor
        case .cancel, .deadline: return .textSupLight
        default: return .textSupLight
        }
    }
    
    var deptBgColor: UIColor {
        switch self {
        case .sucess: return .mainColorLight
        case .cancel, .deadline: return .background
        default: return .textSupLight
        }
    }
    
    var otherColor: UIColor {
        switch self {
        case .sucess, .cancel: return .textLight
        case .deadline: return .textSupLight
        default: return .textSupLight
        }
    }
    var detailTextColor: UIColor {
        switch self {
        case .sucess, .cancel: return .textDark
        case .deadline: return .textSupLight
        default: return .textSupLight
        }
    }
    
    var detailDataColor: UIColor {
        switch self {
        case .sucess, .cancel: return .mainColor
        case .deadline: return .textSupLight
        default: return .textSupLight
        }
    }
    
    var detailRegFeeColor: UIColor {
        switch self {
        case .sucess, .cancel: return .warning
        case .deadline: return .textSupLight
        default: return .textSupLight
        }
    }
    
    var imgName: String {
        switch self {
        case .sucess: return "registration_detail_sucess"
        case .cancel: return "registration_detail_cancel"
        case .deadline: return "registration_detail_deadline"
        default: return " "
        }
    }
}

struct RegistrationModel: HandyJSON {
    var docName: String?
    var deptName: String?
    var patientName: String?
    var scheduleDate: String?
    var scheduleTime: String?
    var regFee: String?
    var flagName: String?
    var flag: Int?
}

struct RegistrationViewModel {
    let model: RegistrationModel
    var docName: String { return model.docName ?? "" }
    var deptName: String { return model.deptName ?? "" }
    var patientName: String { return model.patientName ?? "" }
    var scheduleDate: String { return model.scheduleDate ?? "" }
    var scheduleTime: String { return model.scheduleTime ?? "" }
    var regFee: String { return model.regFee ?? "" }
    var flagName: String { return model.flagName ?? "" }
    var flag: Int { return model.flag ?? -1 }
    var status: RegistrationStatus { return RegistrationStatus(rawValue: flag) ?? .error }

    func scheduleTimeText() -> NSAttributedString {
        let text = scheduleTime + " 时间仅供参考，以医院实际情况为准"
        let attStr = NSMutableAttributedString(string: text)
        let attributes = [NSAttributedString.Key.foregroundColor: status.otherColor, NSAttributedString.Key.font: UIFont.regular(9)]
        attStr.setAttributes(attributes, range: NSRange(location: text.count - 16, length: 16))
        return attStr
    }
}

struct RegistRowModel: HandyJSON {
    var docName: String?
    var scheduleDate: String?
    var patientName: String?
    var flagName: String?
    var deptName: String?
    var flag: Int?
    var outpId: String?
}

struct RegistRowViewModel {
    let model: RegistRowModel
    var docName: String { return model.docName ?? "" }
    var scheduleDate: String { return model.scheduleDate ?? "" }
    var patientName: String { return model.patientName ?? "" }
    var flagName: String { return model.flagName ?? "" }
    var deptName: String { return model.deptName ?? "" }
    var flag: Int { return model.flag ?? -1 }
    var outpId: String { return model.outpId ?? "" }
    var status: RegistrationStatus { return RegistrationStatus(rawValue: flag) ?? .error }
}

struct RegistRecordViewModel {
    let models: [RegistRowModel]
    var numberOfSection: Int { return models.count }
    func rowVm(indexPath: IndexPath) -> RegistRowViewModel {
        return RegistRowViewModel(model: models[indexPath.section])
    }
}

extension RegistrationViewModel {
    
    //挂号预约记录
    static func getRegistrationRecord(success: @escaping (_ models: [RegistRowModel]) -> Void, failure: @escaping (_ errorMsg: String?) -> Void) {
        HttpTool.shared.request(target: ApiManager.registrationRecord, success: { (json, code) in
            switch code {
            case .failure:
                failure(json["message"].description)
            case .success:
                if let models = JSONDeserializer<RegistRowModel>.deserializeModelArrayFrom(json: json["data"].description) as? [RegistRowModel] {
                    success(models)
                } else {
                    failure("请求数剧发生错误，稍后重试")
                }
            }
        }) { (errorMsg) in
            failure(errorMsg)
        }
    }
    
    //新增预约挂号
    static func insertRegistration(scheduleId: String,
                                   patientId: String,
                                   finished: @escaping (_ registrationId: String?, _ msg: String?, _ code: HttpCode) -> Void) {
        let paras = ["scheduleId" : scheduleId,
                     "patientId" : patientId]
        HttpTool.shared.request(target: ApiManager.instertRegistration(paras: paras), success: { (json, code) in
            finished(json["data"].description, json["message"].description, code)
        }) { errorMsg in
            finished(nil, errorMsg, .failure)
        }
    }
    
    //新增预约挂号失败调这个接口取消
    static func deleteRegistration(scheduleId: String,
                                   businessType: String = "3",
                                   patientName: String,
                                   finished: @escaping (_ msg: String?, _ code: HttpCode) -> Void) {
        let paras = ["scheduleId" : scheduleId,
                     "name" : patientName,
                     "businessType" : businessType]
        HttpTool.shared.request(target: ApiManager.deleteRegistration(paras: paras), success: { (json, code) in
            finished(json["message"].description, code)
        }) { errorMsg in
            finished(errorMsg, .failure)
        }
    }
    
    //个人中心门诊挂号记录取消预约
    static func cancelRegistration(scheduleId: String,
                                   finished: @escaping (_ msg: String?, _ code: HttpCode) -> Void) {
        let paras = ["outpId" : scheduleId]
        HttpTool.shared.request(target: ApiManager.cancelRegistration(paras: paras), success: { (json, code) in
            finished(json["message"].description, code)
        }) { errorMsg in
            finished(errorMsg, .failure)
        }
    }
    
    //预约挂号详情
    static func registrationDetail(scheduleId: String,
                                   success: @escaping (_ model: RegistrationModel) -> Void,
                                   failure: @escaping (_ msg: String?) -> Void) {
        let paras = ["outpId" : scheduleId]
        HttpTool.shared.request(target: ApiManager.registrationDetail(paras: paras), success: { (json, code) in
            switch code {
            case .failure:
                failure(json["message"].description)
            case .success:
                if let model = JSONDeserializer<RegistrationModel>.deserializeFrom(json: json["data"].description) {
                    success(model)
                } else {
                    failure("请求数据发生错误，稍后重试")
                }
            }
        }) { errorMsg in
            failure(errorMsg)
        }
    }
}
